# -*- coding:utf-8 -*-
import pygame
import random
import sys

# 初始化游戏
pygame.init()
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("河流划船冒险")

# 颜色定义
WATER_BLUE = (64, 164, 223)
BOAT_BROWN = (139, 69, 19)
OBSTACLE_COLOR = (34, 139, 34)
WHITE = (255, 255, 255)


class Boat:
    def __init__(self):
        self.width = 60
        self.height = 30
        self.x = WIDTH // 2
        self.y = HEIGHT - 100
        self.speed = 5
        self.score = 0

    def draw(self):
        pygame.draw.rect(screen, BOAT_BROWN, (self.x, self.y, self.width, self.height))
        pygame.draw.polygon(screen, BOAT_BROWN, [
            (self.x + self.width, self.y),
            (self.x + self.width + 20, self.y + self.height // 2),
            (self.x + self.width, self.y + self.height)
        ])


class Obstacle:
    def __init__(self):
        self.width = random.randint(50, 120)
        self.height = 20
        self.x = random.randint(0, WIDTH - self.width)
        self.y = -self.height
        self.speed = random.randint(2, 5)

    def draw(self):
        pygame.draw.rect(screen, OBSTACLE_COLOR, (self.x, self.y, self.width, self.height))

    def update(self):
        self.y += self.speed
        return self.y > HEIGHT

    def check_collision(self, boat):
        return (boat.x < self.x + self.width and
                boat.x + boat.width > self.x and
                boat.y < self.y + self.height and
                boat.y + boat.height > self.y)


def main():
    clock = pygame.time.Clock()
    boat = Boat()
    obstacles = []
    spawn_timer = 0
    game_active = True

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYDOWN and not game_active:
                if event.key == pygame.K_r:
                    main()

        if game_active:
            keys = pygame.key.get_pressed()
            if keys[pygame.K_LEFT]: boat.x = max(0, boat.x - boat.speed)
            if keys[pygame.K_RIGHT]: boat.x = min(WIDTH - boat.width, boat.x + boat.speed)

            spawn_timer += 1
            if spawn_timer > 60:
                obstacles.append(Obstacle())
                spawn_timer = 0

            for obstacle in obstacles[:]:
                if obstacle.update():
                    obstacles.remove(obstacle)
                    boat.score += 1
                if obstacle.check_collision(boat):
                    game_active = False

        screen.fill(WATER_BLUE)
        boat.draw()
        for obstacle in obstacles:
            obstacle.draw()

        font = pygame.font.SysFont(None, 36)
        score_text = font.render(f"分数: {boat.score}", True, WHITE)
        screen.blit(score_text, (10, 10))

        if not game_active:
            game_over_text = font.render("游戏结束! 按R键重新开始", True, WHITE)
            screen.blit(game_over_text, (WIDTH // 2 - 150, HEIGHT // 2))

        pygame.display.flip()
        clock.tick(60)


if __name__ == "__main__":
    main()
